package by.alex.dzeshko.lastfmsimple.bo.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import by.alex.dzeshko.lastfmsimple.content.Contract;

import android.util.Log;

public class JSONModel {

	private static final String LOG_TAG = "JSONModel";
	private static final String URL = "url";
	private static final String MBID = "mbid";
	protected static final String NAME = "name";
	private static final String RESULTS = "results";
	private static final String MEGA2 = "mega";
	private static final String EXTRALARGE = "extralarge";
	private static final String LARGE2 = "large";
	private static final String MEDIUM2 = "medium";
	private static final String SMALL2 = "small";
	private static final String TEXT = "#text";
	private static final String IMAGE = "image";
	private static final String SIZE = "size";
	private static final int SMALL = 0;
	private static final int LARGE = 1;
	private static final int MEDIUM = 2;
	private static final int XLARGE = 3;
	private static final int MEGA = 4;

	protected JSONObject jo;

	public JSONModel(JSONObject jsonObject) {
		jo = jsonObject;
	}

	public JSONModel(String jsonString) throws JSONException {
		jo = new JSONObject(jsonString);
	}

	public JSONModel() {
		jo = new JSONObject();
	}

	public String getString(String key) throws JSONException {
		if (jo.isNull(key))
			return null;
		if (jo.has(key))
			return jo.getString(key);
		return null;
	}

	protected String getStringFromObject(String object, String key)
			throws JSONException {
		if (jo.isNull(object))
			return null;
		if (jo.has(object))
			return jo.getJSONObject(object).getString(key);
		return null;
	}

	protected int getInt(String key) throws JSONException {
		if (jo.isNull(key))
			return -1;
		if (jo.has(key))
			return jo.getInt(key);
		return -1;
	}

	protected Long getLong(String key) throws JSONException {
		if (jo.isNull(key))
			return null;
		if (jo.has(key))
			return jo.getLong(key);
		return null;
	}

	protected int getIntFromObject(String object, String key)
			throws JSONException {
		if (jo.isNull(object))
			return -1;
		if (jo.has(object))
			return jo.getJSONObject(object).getInt(key);
		return -1;
	}

	protected JSONObject getValueFromArray(String arrayKey, String objectKey,
			String valueKey) throws JSONException {
		if (arrayKey == null || valueKey == null)
			return null;
		if (jo.isNull(arrayKey))
			return null;
		JSONObject value = null;
		JSONArray ja = jo.getJSONArray(arrayKey);
		for (int i = 0; i < ja.length(); i++) {
			JSONObject temp = ja.getJSONObject(i);
			if (temp.getString(objectKey).equals(valueKey)) {
				value = temp;
				return value;
			}
		}
		return null;
	}

	protected String getName() throws JSONException {
		return getString(NAME);
	}

	protected String getMbid() throws JSONException {
		return getString(MBID);
	}

	protected String getUrl() throws JSONException {
		return getString(URL);
	}

	protected static List<JSONObject> getObjectsList(Object source,
			String matches, String object) throws JSONException {

		if (source == null) {
			return null;
		}
		JSONObject results = ((JSONObject) source).getJSONObject(RESULTS);
		if (results.has(matches)) {
			if(results.getString(matches).equals(" ") || results.getString(matches).equals("\n")) {
				Log.d(LOG_TAG, "no matches");
				return new ArrayList<JSONObject>();
			}
			JSONObject match = results.getJSONObject(matches);
			if (match.has(object)) {
				JSONArray albums = match.getJSONArray(object);
				if (albums.length() == 0) {
					Log.d(LOG_TAG, "matches size 0");
					return new ArrayList<JSONObject>();
				}
				List<JSONObject> list = new ArrayList<JSONObject>(
						albums.length());
				for (int i = 0; i < albums.length(); i++) {
					list.add(albums.getJSONObject(i));
				}
				return list;
			} else
				return new ArrayList<JSONObject>();
		} else
			return new ArrayList<JSONObject>();
	}

	protected String getImage(int size) throws JSONException {
		if (size > XLARGE || size < SMALL)
			throw new IllegalArgumentException();
		if (jo.has(IMAGE)) {
			JSONArray images = jo.getJSONArray(IMAGE);

			HashMap<String, String> urls = new HashMap<String, String>(4);
			for (int i = 0; i < images.length(); i++) {
				JSONObject image = images.getJSONObject(i);
				urls.put(image.getString(SIZE), image.getString(TEXT));
			}
			switch (size) {
			case SMALL:
				return urls.get(SMALL2);
			case MEDIUM:
				return urls.get(MEDIUM2);
			case LARGE:
				return urls.get(LARGE2);
			case XLARGE:
				return urls.get(EXTRALARGE);
			case MEGA:
				return urls.get(MEGA2);
			default:
				return "";
			}
		} else
			return "";
	}

	public String getMarker() throws JSONException {
		String marker = jo.getString(Contract.AlbumsColumns.MARKER);
		Log.d(LOG_TAG, "marker: "+marker);
		return marker;
	}
}
